package org.example.demo2;

/**
 * 使用枚举保存系统业务状态码和业务响应消息
 */
public enum ApiCodeEnum {
    SUCCESS(0,"请求成功"),
    FAILURE(1,"请求失败"),
    PWD_ERROR(2,"密码错误"),
    INVALID_USERNAME(3,"无效账号"),
    ACCOUNT_LOCKED(4,"账号被锁定")

    ;

    //属性
    private int code;
    private String msg;
    //setter和getter方法   Alt+Insert
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
    //有参构造方法(必须私有的[private]，可以省略)
    ApiCodeEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    // 静态方法
    public static ApiCodeEnum getApiCode(int code) {
        ApiCodeEnum[] values = ApiCodeEnum.values();
        for (ApiCodeEnum value : values) {
            if(value.getCode() ==  code) {
                return value;
            }
        }
        return null;
    }

    // 重写方法toString()方法


    @Override
    public String toString() {
        String info = "业务状态码：" + this.code + ",业务响应信息：" + this.msg;
        return info;
    }

    public static void main(String[] args) {
        //枚举名.values()方法:以数组形式返回枚举类型的所有成员
        //枚举名.valueOf()	将普通字符串转换为枚举实例
        ApiCodeEnum[] values = ApiCodeEnum.values();
        for (ApiCodeEnum value : values) {
            System.out.println("常量值：" + value);
        }
    }
}
